package com.fll.fangyiuser.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class RedisUtil {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 向redis中存值
     * @param key 键
     * @param value 值
     * @return 是否存入成功
     */
    public boolean setValue(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            log.error("向redis中存入值时异常===>{}", e.getMessage());
            return false;
        }
    }

    /**
     * 向redis中存值并指定过期时间
     * SECONDS: 秒
     * MINUTES: 分
     * HOURS: 时
     * DAYS: 天
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean setValueTime(String key, Object value, long time) {
        try {
            if (time > 0) {
                //TimeUnit.MINUTES:传入的时间为分钟
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.MINUTES);
            } else {
                redisTemplate.opsForValue().set(key, value);
            }
            return true;
        } catch (Exception e) {
            log.error("设置缓存并指定过期时间异常===>{}", e.getMessage());
            return false;
        }
    }

    /**
     * 根据key获取redis中的值
     * @param key
     * @return
     */
    public Object getValue(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    /**
     * 根据key删除redis中的缓存
     * @param keys 多个key
     * @return
     */
    public void delKey(String... keys) {
        if (keys != null && keys.length > 0) {
            if (keys.length == 1) {
                redisTemplate.delete(keys[0]);
            } else {
                for (String key : keys) {
                    redisTemplate.delete(key);
                }
            }
        }
    }

    /**
     * 判断值是否存在
     * @param key
     * @return
     */
    public boolean haskey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            log.error("redis值不存在===>{}", e.getMessage());
            return false;
        }
    }

    /**
     * 获取redis键的过期时间
     * 0代表永久有效
     * 大于0就剩多少分钟失效
     * @param key
     * @return
     */
    public Long isExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.MINUTES);
    }

    /**
     * 给key加过期时间
     * @param key
     * @param time
     * @return
     */
    public boolean expire(String key, long time) {
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.MINUTES); //分钟
            }
            return true;
        } catch (Exception e) {
            log.error("给旧的缓存设置新的过期时间异常===> {}", e.getMessage());
            return false;
        }
    }

    /**
     * object转集合
     * @param obj
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> List<T> castList(Object obj, Class<T> clazz) {
        List<T> result = new ArrayList<T>();
        if (obj instanceof List<?>) {
            for (Object o : (List<?>) obj) {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }

    /**=====================================================redis操作set集合============================================*/

    /** https://zhidao.baidu.com/question/2019772545073839868.html
     * 存入set集合
     * @param key
     * @param datas
     * @param <?>
     * @param minus
     */
    public Boolean putCacheSet(String key, Set<?> datas, Long minus) {
        try {
            SetOperations<String, Object> opsSet = null;
            if (null != redisTemplate) {
                opsSet = redisTemplate.opsForSet();
                if (null != opsSet) {
                    opsSet.add(key, datas);
                    if (null != minus) {
                        redisTemplate.expire(key, minus, TimeUnit.MINUTES);
                    }
                }
                return true;
            }
        } catch (Exception e) {
            e.getMessage();
            return false;
        }
        return false;
    }

    /**
     * 获取set集合
     * @param key
     */
    public Set<Object> getCacheSet(String key) {
        Set<Object> dataSet = new HashSet<>();
        try {
            SetOperations<String, Object> opsSet = null;
            if (null != redisTemplate) {
                opsSet = redisTemplate.opsForSet();
                if (null != opsSet) {
                    dataSet = opsSet.members(key);
                }
            }
        } catch (Exception e) {
            e.getMessage();
        }
        return dataSet;
    }

    /**
     * 两个key之间取交集
     * @param key1
     * @param key2
     * @return
     */
    public Set<Object> sinterSet(String key1, String key2) {
        Set<Object> dataSet = null;
        try {
            SetOperations<String, Object> opsSet = null;
            if (null != redisTemplate){
                opsSet = redisTemplate.opsForSet();
                if (null != opsSet) {
                    dataSet = opsSet.intersect(key1, key2);
                }
            }
        } catch (Exception e) {
            e.getMessage();
        }
        return dataSet;
    }

}
